package interfaceDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-04-05
 * Time: 16:57
 */
class Money implements Cloneable{
    double money;

    @Override
    public String toString() {
        return "Money{" +
                "money=" + money +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


public class Student implements Person,Comparable,Cloneable{

    String name;
    int age;
    Money m = new Money();

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //调用Object中的clone方法，先将获得引用存入temp
        Student temp = (Student) super.clone();
        //通过temp.m 访问到的是拷贝出的新的对象中的 m 引用
        // this.m 访问到的是当前对象中的 m 引用
        //拷贝出新的一份放到 克隆出的对象中
        temp.m = (Money)this.m.clone();
        return temp;
    }

    @Override
    public void run() {
        System.out.println("学生在跑");
    }

    @Override
    public void eat() {
        System.out.println("学生在吃饭");
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", m=" + m +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        return this.age - ((Student)o).age ;
    }
}
